package com.example.myapplication.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.example.myapplication.MainActivity;
import com.example.myapplication.R;

import java.lang.ref.WeakReference;

/**
 *
 * Created by Lx on 2019/3/22.
 */

public class SplashActivity extends BaseActivity {
    private final TimerHandler mTimerHandler = new TimerHandler();
    private final TimerRunnable mTimerRunnable = new TimerRunnable(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initTimer();
    }

    private void initTimer() {
        mTimerHandler.postDelayed(mTimerRunnable, 3000);
    }

    private static class TimerHandler extends Handler {

    }

    private static class TimerRunnable implements Runnable {
        private final WeakReference<SplashActivity> activityWeakReference;

        TimerRunnable(SplashActivity activity) {
            this.activityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void run() {
            if (activityWeakReference.get() != null) {
                activityWeakReference.get().startActivity(new Intent(activityWeakReference.get(), MainActivity.class));
                activityWeakReference.get().finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTimerHandler.removeCallbacks(mTimerRunnable);
    }
}
